package com.gem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gem.entity.Product;
import com.gem.entity.User;
import com.gem.service.CartService;
import com.gem.service.ProductCategoryService;
import com.gem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import static com.gem.util.PageUtil.PAGESIZE;

/**
 * @author 刘聪
 * Created  on 2021/3/21
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private CartService cartService;

    @GetMapping("/consultation")
    public String consultation() {
        return "product/consultation";
    }


    @RequestMapping("/search")
    public String searchPage(@RequestParam(value = "searchInput", defaultValue = "", required = false) String searchInput,
                             @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                             Model model) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("name", searchInput);
        Page<Product> page = new Page<>(pageNum, PAGESIZE);
        Page<Product> productPage = productService.page(page, wrapper);
        model.addAttribute("Total", productPage.getTotal());

        model.addAttribute("searchInput", searchInput);

//        model.addAttribute("list",productCategoryService.getAllProductCategoryVO());

        model.addAttribute("PageNum", pageNum);
        model.addAttribute("PageTotal", productPage.getPages());
        List<Product> productList = productPage.getRecords();
        model.addAttribute("productList", productList);
        return "product/search";
    }

    /*@PostMapping("/listCategory")
    public String category() {
    }*/

    @GetMapping("/list/{id}")
    public String list(@PathVariable("id") Integer id, Model model) {
        List<Product> categoryId = productService.findByCategoryId(id);
        model.addAttribute("productIdList", productService.findByCategoryId(id));
        return "/list/{id}";
    }

    @GetMapping("/{id}")
    public String productDetail(@PathVariable("id") Integer id, Model model, HttpSession session) {
        Product byId = productService.getById(id);
        model.addAttribute("product", productService.getById(id));
        model.addAttribute("list", productCategoryService.getAllProductCategoryVO());
        User user = (User) session.getAttribute("User");
        if (user == null) {
            model.addAttribute("cartList", new ArrayList<>());
        } else {
            model.addAttribute("cartList", cartService.findAllCartVOByUserId(user.getId()));
        }
        return "/product/productDetail";
    }

    @GetMapping("/change")
    public String change() {
        return "product/change";
    }

    @GetMapping("/suggest")
    public String suggest() {
        return "product/suggest";
    }

}
